package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class WechatServiceImpl implements WechatService {
    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${Wechat.AppID}")
    private String appid;

    @Value("${Wechat.AppSecret}")
    private String secret;


    /**
     * 获取openid
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenid(String code) {
        Map<String, Object> paramMap=new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        paramMap.put("js_code", code);
        String result = HttpUtil.get(REQUEST_URL, paramMap);
        JSONObject jsonObject = JSON.parseObject(result);
        if(ObjectUtil.isNotEmpty(jsonObject.getInteger("errcode"))){
            throw new BaseException(jsonObject.getString("errmsg"));
        }
        return jsonObject.getString("openid");
    }

    /**
     * 获取手机号
     *
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
        //调用方法获取AccessToken
        String accessToken = getAccessToken();
        Map<String, Object> paramMap=new HashMap<>();
        paramMap.put("code", detailCode);
        String result = HttpUtil.post(PHONE_REQUEST_URL + accessToken, JSONUtil.toJsonStr(paramMap));
        JSONObject jsonObject = JSONObject.parseObject(result);
        if(!jsonObject.getInteger("errcode").equals(0)){
            //errcode不为0则说明有错误
            throw new BaseException(jsonObject.getString("errmsg"));
        }
        return jsonObject.getJSONObject("phone_info").getString("phoneNumber");
    }
    /**
     * 获取AccessToken
     *
     * @return
     */
    private String getAccessToken() {
        Map<String, Object> paramMap=new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        //将字符串转换成JSONObject对象。才能拿出里面的值
        JSONObject jsonObject = JSON.parseObject(result);
        if(ObjectUtil.isNotEmpty(jsonObject.getInteger("errcode"))){
            throw new BaseException(jsonObject.getString("errmsg"));
        }
        return jsonObject.getString("access_token");
    }
}
